/*
 * Copyright (c) 2011, Yawning <yawninglol at gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   * Neither the name of the Mew Developers nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package yawning.mew.protwarrior;

import yawning.mew.ProcEffect.Trigger;
import yawning.mew.character.CharacterModel.AttackResult;
import yawning.mew.sim.SimulationEvent;

public class MeleeEvent extends SimulationEvent {
	ProtWarriorSimulator mState;

	public MeleeEvent(ProtWarriorSimulator state) {
		super(state.mSimEngine);

		mState = state;
	}

	@Override
	public void onTick() {
		AttackResult result = mState.mModel.getFrontalWhiteResult();

		if (result == AttackResult.MISS) {
			mNrWhiteMiss++;

			if (mState.mIsLogging) mState.mLog.log("White Swing: MISS\n");
		} else if (result == AttackResult.DODGE) {
			mNrWhiteDodge++;

			if (mState.mIsLogging) mState.mLog.log("White Swing: DODGE\n");
		} else if (result == AttackResult.PARRY) {
			mNrWhiteParry++;

			if (mState.mIsLogging) mState.mLog.log("White Swing: PARRY\n");
		} else {
			double damage = mState.mModel.getWeaponDamage();

			if (result == AttackResult.GLANCE) {
				mNrWhiteGlance++;
				damage *= 0.75;
			} else if (result == AttackResult.CRIT) {
				mNrWhiteCrit++;
				damage *= mState.mModel.multCrit;
			} else {
				mNrWhiteHit++;
			}

			damage *= mState.getDirectDamageMult();
			mWhiteDamage += Math.round(damage);

			if (mState.mIsLogging) mState.mLog.log("White Swing: %s Damage %d\n", result, Math.round(damage));

			if (result == AttackResult.CRIT) {
				if (mState.mDeepWoundsEv.mIsEnabled) mState.mDeepWoundsEv.onTrigger();
				mState.mSimEngine.triggerProc(mState.mActiveTarget, Trigger.WHITECRIT);
			} else {
				mState.mSimEngine.triggerProc(mState.mActiveTarget, Trigger.WHITE);
			}

			// As of Cataclysm, rage generation is normalized.
			int rage = (int) (6.5 * mState.mModel.getBaseWeaponSpeed());
			mState.onRageGainThreat(rage);
			mRageGained += rage;
		}

		setTickInterval(mState.mModel.getMeleeInterval());
	}

	public void start() {
		if (!isUp()) schedule(Double.MAX_VALUE, mState.mModel.getMeleeInterval());
	}

	public void stop() {
		cancel();
	}

	public void reset() {
		schedule(Double.MAX_VALUE, mState.mModel.getMeleeInterval());
	}

	long mWhiteDamage = 0;
	int mNrWhiteMiss = 0;
	int mNrWhiteDodge = 0;
	int mNrWhiteParry = 0;
	int mNrWhiteGlance = 0;
	int mNrWhiteCrit = 0;
	int mNrWhiteHit = 0;

	long mRageGained = 0;
}
